﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Xml.Linq;
using DVK.SP2007.UniversalClient;
using DVK.SP2007.Constants;

namespace DVK.SP2007
{
    /// <summary>
    /// Maps IDvkMessage to SPListItem. This happens, when SPListItem for outbox list is created.
    /// </summary>
    public class OutgoingMessageToListitemMapper : IMessageMapper<SPListItem>
    {
        private SPListItem _listItem;
        private IDvkMessage _message;
        private SPListItem _documentLibraryItem;
        private IDvkResourceManager _resourceManager;

        public OutgoingMessageToListitemMapper(SPListItem documentLibraryItem, IDvkMessage dvkMessage, SPListItem newOutboxItem, IDvkResourceManager resourceManager)
        {
            this._listItem = newOutboxItem;
            this._message = dvkMessage;
            this._documentLibraryItem = documentLibraryItem;
            _resourceManager = resourceManager;
        }

        #region IMessageConverter<SPListItem> Members

        /// <summary>
        /// Maps metadata from somewhere into the object instance of type T.
        /// <remarks>
        /// Normally this means mapping information from different location into one format (object).
        /// e.g. to IDvkMessage or Inbox SPListItem.
        /// </remarks>
        /// </summary>
        /// <returns></returns>
        public SPListItem Map()
        {
            //Main metadata fields, on to XML
            _listItem[Fields.SenderOrganisationCode.Name] = _message.Sender.Organisation.Code;
            _listItem[Fields.SenderOrganisationName.Name] = _message.Sender.Organisation.Name;
            _listItem[Fields.SenderPersonCode.Name] = _message.Sender.Person.Code;
            _listItem[Fields.SenderPersonName.Name] = _message.Sender.Person.Name;
            _listItem[Fields.SendingStatus.Name] = (int)_message.SendingStatus;
            _listItem[Fields.SendingDate.Name] = _message.SendingDate;
            _listItem[Fields.Title.Name] = _message.Title;
            _listItem[Fields.FolderName.Name] = _message.FolderName;
            _listItem[Fields.DocumentLibraryItemId.Name] = _documentLibraryItem.ID;
            //Staatuste tekstid paika listitemil
            _listItem[Fields.SendingStatusText.Name] = _resourceManager.GetResourceBasedOnEnumElement<DvkSendingStatus>(_message.SendingStatus);
            //XML -s
            _listItem[Fields.DvkMetainfoXml.Name] = _message.DataXml.Metainfo.ToString();
            _listItem[Fields.DvkTransportXml.Name] = _message.DataXml.Transport.ToString();
            _listItem[Fields.DvkMetaXml.Name] = _message.DataXml.MetaXml.ToString();
            _listItem[Fields.ToBeProcessed.Name] = true;
            _listItem[Fields.StatusUpdateNeeded.Name] = _message.StatusUpdateNeeded;
            _listItem[Fields.CaseName.Name] = _message.CaseName;
            return _listItem;
        }

        #endregion
    }
}
